<template>
	<view class="page">
		<view class="selectBox">
			<view class="selectItem" v-for="(item,index) in buys" :key="index" @click="handleSelectItem(index)">

				<view class="image">
					<image :src="imgBaseUrl3+item.pic"></image>
				</view>
					
					<view class="infoBox">
						
						<view class="name">
							{{item.commodityName}}
							<text >{{item.specificationNames}}</text>
						</view>
						
						<view class="row">
						<text >{{item.num}} 件</text>
						<text class="moneyText red">￥{{item.price}}</text>
						</view>
						
					</view>
					
			</view>
		</view>
		<view class="row">
			<view class="label">
				{{'合计'}}:
			</view>
			<view class="text red" style="font-weight: 1200;font-size: 30px;">
				￥{{totalMoney}}
			</view>
		</view>
		
		<view class="textTitle">
			{{language.topup.label4}}
		</view>
		<view class="payType">
			<u-icon name="weixin-fill" color="#10d269" size="30"></u-icon>
			<text>
				{{language.topup.label5}}
			</text>
			<u-icon name="checkmark" color="#2979ff" size="28"></u-icon>
		</view>
		<view class="btn" @click="handleTopup">
			<text>{{'支付'}}</text>
		</view>
		
	</view>
</template>

<script>
	import {confirmCommodityPrice,rechargeApi} from '@/common/api/commonApi/uniformshop.js'
	export default {
		data() {
			return {
				buys:[
				],
				selectMoney: '',
				deductionAmount: '',
				serviceCharge :'',
				cardSn : '',
				selectInfoAccount : uni.getStorageSync('selectInfoAccount'),
				themeId :'',
				subjectIdList : [],
				payBool: false,
				isCart:false,
				
			}
		},
		computed: {
			totalMoney(){
				let num = 0
				console.log(this.buys)
				this.buys.forEach(item=>{
						num+=item.price
				})
				return num
			}
		},
		methods: {
			handleSelectItem(index){
				console.log(this.buys[index]);
				let item = this.buys[index]
				this.$set(this.buys,index,item)
			},
			calTotal(selectList){
				let num = 0
				selectList.forEach(item=>{
						num+=item.price
				})
				return num
			},
			getPayParams(selectList){
				for(let i = 0;i<selectList.length;i++){
					selectList[i].commoditySpecificationId = selectList[i].id;
				}
				return selectList
			},
			removeFromCart(params){
				let shopcartstr = uni.getStorageSync("shopcart");
				
				
				let shopcartjson = JSON.parse(shopcartstr);
				console.log(shopcartjson)
				let scfi = shopcartjson.filter(item=>{
					for(let i = 0;i<params.length;i++){
						if(params[i].id==item.id){
							return false;
						}else{
							return true;
						}
					}
				});
				console.log(scfi)
				uni.setStorageSync("shopcart",JSON.stringify(scfi));
			},
			handleTopup(){
				//if(!this.totalMoney) return this.setMessage('请先选择课程')
				let  selectList = this.buys;
				console.log(selectList)
				let totalm = this.calTotal(selectList);
				let params = this.getPayParams(selectList);
				console.log(totalm)
				console.log(params)
				let data = {
					commodityBuys : params,
					amount:totalm,
					//amount:0.01,
				    openid:uni.getStorageSync('openId'),
				}
				if(this.payBool) return
				this.payBool = true
				rechargeApi(data).then(res=>{
						uni.requestPayment({
							provider: 'wxpay',
							nonceStr :  res.result.nonce_str,
							package : `prepay_id=${res.result.prepay_id}`,
							paySign : res.result.sign,
							signType : 'MD5',
							timeStamp: String(res.result.timeStamp),
							success:() =>{
									this.setMessage('支付成功')
									this.removeFromCart(params);
									setTimeout(()=>{
										uni.reLaunch({
											url:'/pages/index/index'
										})
									},1000)
							},
							fail: (err) => {
								console.log(err);
								this.setMessage('支付取消或失败')
								
							
							},
							complete:()=> {
								this.payBool = false
							}
						})
				
				})
			},
			confirmPrice(){
				let params = {};
				params.commoditySpecificationId = buy.id;
				params.num = buy.num;
				
				//let res = await confirmCommodityPrice()
			},
		},
		onLoad({id}) {
			//this.themeId = id;
			// this.getList()
			let temp = this.$Route.query.item;
			this.buys = temp;

			//confirmPrice();
		
		},
		onShow() {
			
		},
	}
</script>

<style lang="scss" scoped>
.page{
	background-color: #fff;
	height: 100vh;
	.text{
		color: #333;
	}
	.row{
		display: flex;
		align-items: center;
		height: 90rpx;
		border-bottom: 2rpx solid #f1f1f1;
		&:nth-child(1){
			border-top: 2rpx solid #f1f1f1;
		}
		.label{
			margin: 0 30rpx;
		}
	
	}
	.red{
		color: #fe415c !important;
	}
	.money{
		height: 100rpx;
		@include flexCenter;
		font-size: 36rpx;
		border-bottom: 2rpx solid #f1f1f1;
		text{
			font-size: 28rpx;
			color: #333;
			margin-left: 5rpx;
		}
	}
	.textTitle{
		margin-top: 40rpx;
		font-size: 30rpx;
		padding-left: 50rpx;
		color: #000;
	}
	.payType{
		display: flex;
		align-items: center;
		height: 110rpx;
		padding: 0 50rpx;
		width: 100%;
		image{
			width: 50rpx;
			height: 60rpx;
		}
		text{
			color: #000;
			flex: 1;
			padding-left: 20rpx;
			white-space: nowrap;
			font-size: 30rpx;
		}
	}
	.btn{
		margin: 20rpx auto 0;
		background: #2d8cf6;
		@include flexCenter;
		width: 90vw;
		height: 92rpx;
		border-radius: 10rpx;
		text{
			color: #fff;
		}
	}
	
	.selectBox{
		min-height: 700rpx;
		padding-top: 30rpx;
		.selectItem{
			background-color: #fff;
			margin: 0 30rpx 30rpx;
			border-radius: 8rpx;
			padding: 10rpx;
			box-shadow: 0px 0px 20rpx 0px rgba(102,102,102,0.35);
			padding: 20rpx;
			height: 140rpx;
			display: flex;
			.image{
				width: 100rpx;
				height: 100rpx;
				border-radius: 20rpx;
				image{
					width: 100rpx;
					height: 100rpx;
				}
			}
			.infoBox{
				display: flex;
				align-items: center;
				justify-content: space-evenly;
				flex: 1;
				text{
					color: #c2c2c2;
					// flex: 1;
					display: flex;
					align-items: center;
				}
				.name{
					font-size: 36rpx;
				}
				.moneyText{
					// flex: 1;
					font-size: 38rpx;
				}
			}
			
		}
	}

	.tipBtn{
		color: #0072e5;
		padding:  5rpx 10rpx;
		border: 2rpx solid #0072e5;
		border-radius: 10rpx;
	}
}
</style>
